package lc1993929.com.secret.atys;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import lc1993929.com.secret.Config;
import lc1993929.com.secret.R;
import lc1993929.com.secret.net.Comment;
import lc1993929.com.secret.net.GetComment;
import lc1993929.com.secret.net.PubComment;
import lc1993929.com.secret.utils.MD5Tools;

/**
 * Created by LiuChang on 2017/5/11.
 */

public class AtyMessage extends AppCompatActivity {

    private String phone_md5, msg, msgId, token;
    private TextView tvMessage;
    private EditText etComment;
    private AtyMessageCommentListAdapter adapter;
    private ListView messageListView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_message);

        tvMessage = (TextView) findViewById(R.id.tvMessage);
        etComment = (EditText) findViewById(R.id.etAddComment);
        messageListView = (ListView) findViewById(R.id.messageList);

        Intent data = getIntent();
        phone_md5 = data.getStringExtra(Config.KEY_PHONE_MD5);
        msg = data.getStringExtra(Config.KEY_MSG);
        msgId = data.getStringExtra(Config.KEY_MSG_ID);
        token = data.getStringExtra(Config.KEY_TOKEN);
        adapter = new AtyMessageCommentListAdapter(this);
        messageListView.setAdapter(adapter);

        tvMessage.setText(msg);

        getComments();

        findViewById(R.id.btnSendComment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etComment.getText())) {
                    Toast.makeText(AtyMessage.this, R.string.comment_content_can_not_be_empty, Toast.LENGTH_LONG).show();
                    return;
                }

                final ProgressDialog pd = ProgressDialog.show(AtyMessage.this, getResources().getString(R.string.connecting), getResources().getString(R.string.connecting_to_server));
                new PubComment(MD5Tools.MD5(Config.getCachedPhoneNum(AtyMessage.this)), token, etComment.getText().toString(), msgId, new PubComment.SuccessCallback() {
                    @Override
                    public void onSuccess() {
                        pd.dismiss();
                        etComment.setText("");
                        getComments();
                    }
                }, new PubComment.FailCallback() {
                    @Override
                    public void onFail(int errorCode) {
                        pd.dismiss();
                        if (errorCode == Config.RESULT_STATUS_INVALID_TOKEN) {
                            startActivity(new Intent(AtyMessage.this, AtyLogin.class));
                            finish();
                        } else {
                            Toast.makeText(AtyMessage.this, R.string.fail_to_publish_comment, Toast.LENGTH_LONG).show();
                        }
                    }
                });

            }
        });

    }

    private void getComments() {
        final ProgressDialog pd = ProgressDialog.show(this, getResources().getString(R.string.connecting), getResources().getString(R.string.connecting_to_server));
        new GetComment(phone_md5, token, msgId, 1, 20, new GetComment.SuccessCallback() {
            @Override
            public void onSuccess(String msgId, int page, int perpage, List<Comment> comments) {
                pd.dismiss();
                adapter.clear();
                adapter.addAll(comments);
            }
        }, new GetComment.FailCallback() {
            @Override
            public void onFail(int errorCode) {
                pd.dismiss();

                if (errorCode == Config.RESULT_STATUS_INVALID_TOKEN) {
                    startActivity(new Intent(AtyMessage.this, AtyLogin.class));
                    finish();
                } else {
                    Toast.makeText(AtyMessage.this, R.string.fail_to_get_comment, Toast.LENGTH_LONG).show();
                }
            }
        });
    }


}
